<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="leadOrChanceLabel + '选择'" class="tl-dialog" width="1200px" @open="open" @close="close" top="45px" :append-to-body="true" :modal-append-to-body="true">
    <div style="padding-bottom:10px">
      <!-- 查询界面 -->
      <!-- 列表界面 -->
      <div>
        <!-- 列表选择 -->
        <div class="table-alert-div" ref="selector">
          <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
            <template slot="title">
              已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRows.length }}</a
              >项&nbsp;&nbsp;
              <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
            </template>
          </el-alert>
        </div>
        <!--表格渲染-->
        <el-table class="tl-table" ref="mainTable" :height="tableHeight" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" :row-key="getRowKeys" border fit :row-class-name="doChangeRowClassName">
          <el-table-column type="selection" width="55px" :reserve-selection="true" v-if="isSingleSelect !== false"> </el-table-column>
          <el-table-column prop="CODE" :label="leadOrChanceLabel + '编号'" width="200px" sort-by="CODE" align="center" header-align="center"> </el-table-column>
          <el-table-column prop="PROJECT_NAME" label="项目名称" min-width="300px" sort-by="PROJECT_NAME" align="left" header-align="center"></el-table-column>
          <el-table-column prop="CLIENT_NAME" label="客户名称" width="300px" sort-by="CLIENT_NAME" align="left" header-align="center"></el-table-column>
          <el-table-column prop="DISP_CLIENT_LEVEL" label="客户级别" width="100px" sort-by="DISP_CLIENT_LEVEL" align="center" header-align="center"></el-table-column>
          <el-table-column  prop="OUR_PROCDUCT_FUND" label="金额（万元）" min-width="120px" sort-by="OUR_PROCDUCT_FUND" align="right" header-align="center"></el-table-column>
          <el-table-column prop="CLIENT_DEPT" label="客户部门" width="300px" sort-by="CLIENT_DEPT" align="left" header-align="center"></el-table-column>
        </el-table>
        <!-- 列表分页 -->
        <el-row class="pagebar" ref="pager">
          <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
        </el-row>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doConfirm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_ref_list } from '../../../mixins/mixin_ref_list'
/** 子组件 */
import { validateNull } from '@tapui/utils/validate'
/** 工具类 */
import { filterObjectNull, rmbFormat } from '@tapui/utils/util'
import { queryDataCode } from '@tapui/api/dataCode'
import { formateTec } from '@/utils/global'
import { getAction, putAction, postAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  name: 'TlMpmProjectRef',
  components: {},
  mixins: [mixin_ref_list],
  data() {
    return {
      description: '项目立项参照页面',
      // 默认Key字段名
      fieldDefaultKey: 'id',
      // 默认显示字段名
      fieldDefaultValue: 'name',
      selectedData: [],
      // url配置
      url: {
        list: '/mpm/project/changeList'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'sortcode',
        sord: 'asc'
      },
      ltcinfo: {},
      tableHeight: '370px'
    }
  },
  props: {
    chanceclue2: {
      type: String
    },
    leadOrChanceLabel: {
      type: String
    },
    usercode: {
      type: String,
      default: ''
    },
    origincode: {
      type: String,
      default: ''
    }
  },
  methods: {
    getRowKeys(row) {
      return row.id
    },
    open() {
      this.initRef()
    },
    /** 查询数据 */
    loadData(paramMap) {
      if (!this.url.list) {
        this.$tap.alert('请设置查询数据地址!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (validateNull(paramMap) === false && Object.keys(paramMap).indexOf('currentPage') > -1) {
        this.pageInfo.currentPage = paramMap['currentPage']
      }
      // 查询条件
      var params = this.getQueryParams()
      this.loading = true
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            this.datas = res.result.dataList
            if (this.chanceclue2 !== '') {
              var chancle = this.chanceclue2.split(',')
              setTimeout((_) => {
                for (let j = 0; j < chancle.length; j++) {
                  for (let i = 0; i < this.datas.length; i++) {
                    if (chancle[j] === this.datas[i].CODE) {
                      this.$refs.mainTable.toggleRowSelection(this.datas[i], true)
                    }
                  }
                }
              }, 50)
            }
            this.pageInfo.totalCount = res.result.totalCount
            this.$nextTick(() => {
              if (this.isSingleSelect === false) {
                if (validateNull(this.externalSelectedRowKeys) === false) {
                  let externalSelectedRowKeyArray = this.externalSelectedRowKeys.split(',')
                  this.datas.forEach((item) => {
                    if (typeof item[this.fieldKeyComputed] !== 'undefined' && externalSelectedRowKeyArray.includes(item[this.fieldKeyComputed].toString())) {
                      this.$refs.mainTable.toggleRowSelection(item)
                    }
                  })
                }
              }
            })
          } else {
            this.$tap.alert(res.message)
          }
          this.loading = false
        })
        .catch((err) => {
          this.loading = false
        })
    },
    /** 获取查询参数 */
    getQueryParams() {
      let param = {}
      Object.assign(param, this.queryParam, this.externalQueryParam, this.filtersInfo, this.sorterInfo)
      param.currentPage = this.pageInfo.currentPage
      param.pageSize = this.pageInfo.pageSize
      param.usercode = this.usercode
      param.origincode = this.origincode
      return filterObjectNull(param)
    }
  }
}
</script>

<style lang="scss" scoped>
/** 表格上边、左边的线 */
.el-table--border,
.el-table--group {
  border: none;
}
/**  表格下边的线 */
.el-table--border::before,
.el-table--group::before {
  width: 0;
}
/**  表格右边的线 */
.el-table--border::after,
.el-table--group::after {
  height: 0;
}
/** “暂无数据”居中显示 */
.tl-table {
  /deep/ .el-table__empty-text {
    text-align: center;
  }
  // 控制边框
  /deep/ .gutter {
    display: none !important;
  }
  /deep/ .el-table__header-wrapper .el-table__header {
    table-layout: auto !important;
  }
  /deep/ .el-table__body-wrapper .el-table__body {
    table-layout: auto !important;
  }
}
</style>
